/**
/* Copyright (C) 2011, Catdroid, Bernat Borras
/* 
/* This program is free software; you can redistribute it and/or
/* modify it under the terms of the GNU General Public License
/* as published by the Free Software Foundation in its version 2
/* of the License.
/* 
/* This program is distributed in the hope that it will be useful,
/* but WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
/* 
/* @author Bernat Borras (@bbpp)
 */

package com.curs.catdroid_demo_life_2;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import com.android.dataframework.DataFramework;
import com.android.dataframework.Entity;

public class MainActivity extends FragmentActivity {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	private List<MiDato> mDatos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		/**
		 * Inicializamos la base de datos
		 */

		try {
			DataFramework.getInstance().open(this, getPackageName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		mDatos = getData();
		preparePager();

	}

	private List<MiDato> getData() {

		// Cargamos una lista de entidades de la base de datos
		List<Entity> entidades = DataFramework.getInstance().getEntityList(
				"Datos");

		// Creamos un array vacio para guardar nuestros datos
		List<MiDato> misDatos = new ArrayList<MiDato>();

		// Recorremos los datos de la BBDD para trabajar con ellos
		for (Entity ent : entidades) {
			long id = ent.getId();
			String nombre = ent.getString("Nombre");
			String url = ent.getString("URL");

			misDatos.add(new MiDato(id, nombre, url));
		}

		return misDatos;

	}

	private void preparePager() {
		// Create the adapter that will return a fragment for each of the three
		// primary sections
		// of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager(), mDatos);

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
	}
}
